
<template>
  <pageTabelLayout>
    <template #pageHeader>
      <div class="search-form divClass" v-if="headerShow" style="display: inline-block">
        <div>
          <a-form size="small" :model="pageData" layout="inline" label-align="right" :label-col-props="{ span: 8 }"
            :wrapper-col-props="{ span: 16 }">
            <a-form-item label="织布单号">
              <a-input class="bacClass" v-model="pageData.weavCode" @press-enter="huiche" allow-clear
                style="width: 200px" />
            </a-form-item>

            <a-form-item label="生产单号">
              <a-input class="bacClass" v-model="pageData.produceCode" @press-enter="huiche" allow-clear
                style="width: 200px" />
            </a-form-item>

            <a-form-item label="坯布编号">
              <a-input class="bacClass" v-model="pageData.greyCode" @press-enter="huiche" allow-clear
                style="width: 200px" />
            </a-form-item>

            <a-form-item label="损耗布类型">
              <a-input class="bacClass" v-model="pageData.lossclothType" @press-enter="huiche" allow-clear
                style="width: 200px" />
            </a-form-item>

            <a-form-item label="机台号">
              <a-input class="bacClass" v-model="pageData.machineCode" @press-enter="huiche" allow-clear
                style="width: 200px" />
            </a-form-item>

            <a-form-item label="日期" label-col-flex="66px">
              <a-date-picker  popup-container="html"  :style="{ width: '228px' }" class="bacClass" v-model="pageData.dateStart" />
            </a-form-item>
            <a-form-item label="至" label-col-flex="66px">
              <a-date-picker  popup-container="html"  :style="{ width: '242px' }" class="bacClass" v-model="pageData.dateEnd" />
            </a-form-item>
          </a-form>
        </div>
      </div>
      <div class="my-divisders">
        <a-divider orientation="center">
          <icon-double-up v-if="headerShow" @click="headerShow = false" :strokeWidth="2" :size="18" />
          <icon-double-down v-else @click="headerShow = true" :strokeWidth="2" :size="18" />
        </a-divider>
      </div>
      <div class="my-tool-bar">
        <Toolbar :items="pageData.btnList" @select="selectHandler" @export="exports" @del="deleteHandler">
        </Toolbar>
      </div>
    </template>
    <template #pageTabel>
      <a-space style="float: right; z-index: 9999"> </a-space>
      <a-spin :loading="pageData.pageLoading" tip="加载中" dot style="height: 100%; width: 100%; z-index: 999">
        <AgGridVuesCopys :rowHeight="33" :headerHeight="33" :suppressCopyRowsToClipboard="true"
          :suppressDragLeaveHidesColumns="true" rowSelection="multiple" @grid-ready="gridReady"
          @columnResized="onColumnResized" @columnMoved="onColumnMoved" @columnVisible="onColumnVisible"
          style="height: 100%" :defaultColDef="defaultColDef" class="ag-theme-alpine" :rowData="table.dataList"
          :columnDefs="tc1" :localeText="localeText1">
        </AgGridVuesCopys>
      </a-spin>
    </template>
    <paginationCopys :total="total" show-total show-jumper show-page-size @change="PageSize" @page-size-change="change1"
      style="justify-content: center" :page-size="pagination.pageSize" :current="pagination.page"
      :page-size-options="pagingList1" />
    <a-drawer :drawer-style="{ zIndex: 9999 }" :mask-closable="false" :width="400" :visible="pageData.showAdd"
      unmountOnClose @ok="sideOk" @cancel="pageData.showAdd = false" cancel-text="关闭" ok-text="保存">
      <template #title>
        {{ pageData.sideTitle }}
      </template>
      <div>
        <a-form ref="formRef" layout="horizontal" :model="dataForm">
          <a-form-item label-col-flex="80px" label="损耗布重量">
            <a-input class="bacClass" v-model="dataForm.lossWeight" />
          </a-form-item>
        </a-form>
      </div>
    </a-drawer>
  </pageTabelLayout>
</template>

<script lang="ts">
import { post, get } from '@/api/http'
import { useTable, useRowKey, useTableColumn, usePagination, useRowSelection } from '@/hooks/table'
import printJS from 'print-js'
import QrcodeVue from 'qrcode.vue'
import ModalDialog from '@/components/ModalDialog.vue'
import { ref, reactive, onMounted, onActivated } from 'vue'
import Toolbar from '@/components/Toolbar.vue'
import { ModalDialogType } from '@/types/components'
import { Form, Message, Modal } from '@arco-design/web-vue'
import { FormInstance } from '@arco-design/web-vue/es/form'
import emitter from '@/hooks/useEventBus'
import validate from '@/hooks/useValidate'
import { DatasetComponent } from 'echarts/components'
import { AnyARecord, AnyTxtRecord } from 'dns'
// import { baseURL } from '../../../api/axios.config'
import axios from 'axios'
import { url } from 'inspector'
import { EXELTABLE } from '@/utils/exportEXCEL';
import usePermission from '@/store/modules/permission'

import PageTabelLayout from '@/components/pageTabelLayout.vue'
import WHService, { Warhouse, CompanyList, WhTypeField } from '@/service/wh'
import WHStore from '@/store/modules/wh'
import { localeText } from '@/api/aggridZw'
import { pagingList } from '@/api/paging'
import { right } from '@popperjs/core'
import { saveOrUpdateGridState as saveOrUpdateGridState01 } from '@/hooks/useGridState'
interface ClothInfo {
  id: any
  lossWeight: any
}

export default {
  name: 'LossClothRegistrationDemo',
  setup() {
    const dataForm = reactive<ClothInfo>({
      id: null,
      lossWeight: '',
    })
    const headerShow = ref<boolean>(true)
    const total = ref(10)
    const page = ref(1)
    const size = ref(10)
    function PageSize(val: any) {
      pagination.page = val
      doRefresh()
    }

    function change1(val: any) {
      pagination.pageSize = val
      doRefresh()
    }
    let id = ref(1)
    //查询

    function doRefresh() {
      pageData.pageLoading = true
      updateGridState01()
      post({
        url: '/qms/LossclothRegister/getPage',
        data: {
          page: pagination.page,
          size: pagination.pageSize,
          deleteState: 0,

          weavCode: pageData.weavCode,
          produceCode: pageData.produceCode,
          greyCode: pageData.greyCode,
          lossclothType: pageData.lossclothType,
          machineCode: pageData.machineCode,
          beginDate: pageData.dateStart,
          endDate: pageData.dateEnd,
        },
      })
        .then(({ data, code }: any) => {
          if (code === 200) {
            // data.list.filter(function(o:any){

            // })
            table.handleSuccess(data.list)
            gridOption.value.setRowData(data.list)
            total.value = data.total
          }
        })
        .catch(console.log)
        .finally(() => {
          pageData.pageLoading = false
        })
    }
    const huiche = () => {
      doRefresh()
    }
    const { selectedRowKeys, onSelectionChange } = useRowSelection()
    const selectedRowKeysChecked = useRowSelection()
    const pagination = usePagination(doRefresh)
    const table = useTable()
    const table1 = useTable()
    const rowKey = useRowKey('id')
    const tableColumns = useTableColumn([
      {
        title: '公司',
        key: 'outSouringCompany',
        dataIndex: 'outSouringCompany',
        width: 150,
      },
      {
        title: '序号',
        key: 'serialNo',
        dataIndex: 'serialNo',
        width: 150,
      },
      {
        title: '织布单号',
        key: 'weavCode',
        dataIndex: 'weavCode',
        width: 150,
      },
      {
        title: '生产单号',
        key: 'produceCode',
        dataIndex: 'produceCode',
        width: 150,
      },
      {
        title: '坯布编号',
        key: 'greyCode',
        dataIndex: 'greyCode',
        width: 150,
      },
      {
        title: '损耗布类型',
        key: 'lossclothType',
        dataIndex: 'lossclothType',
        width: 150,
      },

      {
        title: '机台号',
        key: 'machineCode',
        dataIndex: 'machineCode',
        width: 150,
      },

      {
        title: '损耗布重量',
        key: 'lossWeight',
        dataIndex: 'lossWeight',
        width: 150,
      },
      {
        title: '创建人',
        key: 'createName',
        dataIndex: 'createName',
        width: 150,
      },
      {
        title: '创建时间',
        key: 'createTime',
        dataIndex: 'createTime',
        width: 200,
      },
      {
        title: '操作',
        key: 'act',
        slotName: 'act',
        width: 80,
        fixed: 'right',
      },
    ])

    //获取公司部门组列表
    const defaultColDef = {
      sortable: true,
      resizable: true,
      filter: true,
      // flex: 1
    }

    let tc1: any[] = tableColumns.map((o) => {
      let obj = {
        headerName: o.title,
        field: o.key,
        pinned: o['fixed'],
        tooltipField: o.key,
      }
      if (o.slotName === 'act') {
        obj['resizable'] = false
        obj['filter'] = false
        obj['sortable'] = false
        obj['lockPosition'] = 'right'
        obj['width'] = 80
        obj['cellRenderer'] = {
          template: `<a-space><el-button style="color:#165dff;" @click="a" class="gesture" link v-if="f">修改</el-button></a-space>`,
          setup(props: any) {
            const a = () => {
              editHandler(props.params.data)
            }
            let f = permissionStore.hasPermisson('shbdj.xg')
            return {
              a,
              ainfo: props.params.data,
              f,
            }
          },
        }
      }
      return obj
    })
    let a1 = [
      {
        showRowGroup: 'hide_def',
        headerName: '',
        checkboxSelection: true,
        headerCheckboxSelection: true,
        pinned: 'left',
        width: 50,
        resizable: false,
        filter: false,
        sortable: false,
        minWidth: 50,
        maxWidth: 50,
        lockPosition: 'left',
      },
    ]
    tc1 = a1.concat(tc1)

    const pageData = reactive<{
      pageLoading: boolean
      produceCode: any
      greyCode: any
      lossclothType: any
      machineCode: any
      dateStart: any
      dateEnd: any
      weavCode: any
      rHeight: number | undefined
      showAdd: boolean
      sideTitle: string
      btnList: any[]
    }>({
      sideTitle: '修改损耗布重量',
      pageLoading: false,
      weavCode: '',
      produceCode: '',
      greyCode: '',
      lossclothType: '',
      machineCode: '',
      dateStart: '',
      dateEnd: '',
      rHeight: 0,
      showAdd: false,
      btnList: [],
    })

    onMounted(() => {
      doRefresh()
      emitter.on('LossClothRegistrationDemo', () => {
        // selCxAll({});
        doRefresh()
      })
      pageData.rHeight = document.getElementsByClassName('main-section')[0]?.clientHeight
      //弹出层的高
      if (pageData.rHeight !== undefined) {
        pageData.rHeight = pageData.rHeight - 50
      }
    })
    let isIDs: any = ref(1)

    // 未处理分页查询
    const selectHandler = () => {
      pagination.page = 1
      loadings.value = true
      doRefresh()
    }

    let loadings = ref(false)

    // 一表格准备事件
    const gridOption = ref()
    const colState = ref()
    const gridReady = (params: any) => {
      gridOption.value = params.api
      colState.value = params.columnApi
    }
    const whList = ref<any[]>([])
    const whService = new WHService()
    const whTypeList = whService.whTypeList
    const whTypeFieldList = whService.whTypeFieldList
    const companyList: any = whService.companyList
    // 表格准备事件
    const gridOption1 = ref()
    const gridReady1 = (params: any) => {
      gridOption1.value = params.api
    }
    const defaultColDef1 = {
      sortable: true,
      resizable: true,
      filter: true,
      // flex: 1
    }
    const formRef = ref<FormInstance>()
    const sideOk = () => {
      post({
        url: '/qms/LossclothRegister/update',
        data: [dataForm],
      }).then((res: any) => {
        if (res.code === 200) {
          formRef.value?.resetFields()
          pageData.showAdd = false

          Message.success({
            content: '修改成功',
            duration: 5000,
          })
          doRefresh()
        } else {
          Message.error({
            content: res.message,
            duration: 5000,
          })
        }
      })
    }
    const deleteHandler = () => {
      if (gridOption.value.getSelectedRows().length === 0) {
        Message.error('请先勾选')
        return
      }
      Modal.confirm({
        title: '系统提示',
        content: '确认删除选中的数据?',
        onOk() {
          post({
            url: '/qms/LossclothRegister/delete',
            data: gridOption.value.getSelectedRows().map((o: any) => {
              return o.id
            }),
          }).then(({ code, data, message }: any) => {
            if (code === 200) {
              Message.success({
                content: '删除成功',
                duration: 5000,
              })

              pagination.page = 1
              doRefresh()
            } else {
              Message.error({
                content: '删除失败',
                duration: 5000,
              })
            }
          })
        },
      })
    }
    const editHandler = (record: any) => {
      for (var key in record) {
        dataForm[key] = record[key]
      }

      pageData.showAdd = true
    }

    //导出
    const exports = () => {
      post({
        url: '/qms/LossclothRegister/getExportExcel',
        data: {
          deleteState: 0,
          weavCode: pageData.weavCode,
          produceCode: pageData.produceCode,
          greyCode: pageData.greyCode,
          lossclothType: pageData.lossclothType,
          machineCode: pageData.machineCode,
          beginDate: pageData.dateStart,
          endDate: pageData.dateEnd,
        },
      })
        .then(({ data, code }: any) => {
          if (code === 200) {
            EXELTABLE(tc1, data, '损耗布登记')
          }
        })
    }


    const pagingList1: any = ref(pagingList)
    const localeText1: any = ref(localeText)
    // ['查询', '新增', '删除', '导出', '导入']
    const permissionStore = usePermission()

    if (permissionStore.hasPermisson('shbdj.cx')) {
      pageData.btnList.push('查询')
    }
    if (permissionStore.hasPermisson('shbdj.dc')) {
      pageData.btnList.push('导出')
    }
    if (permissionStore.hasPermisson('shbdj.sc')) {
      pageData.btnList.push('删除')
    }
    const { saveGridState: saveGridState01, updateGridState: updateGridState01 } = saveOrUpdateGridState01('LossClothRegistrationDemo-1', gridOption, colState, tc1)
    const onColumnMoved = () => {
      saveGridState01()
    }
    const onColumnResized = () => {
      saveGridState01()
    }
    const onColumnVisible = () => {
      saveGridState01()
    }
    return {
      saveOrUpdateGridState01,
      saveGridState01,
      updateGridState01,
      onColumnMoved,
      onColumnResized,
      onColumnVisible,
      colState,
      pagingList1,
      localeText1,
      huiche,
      editHandler,
      gridReady1,
      defaultColDef1,
      print,
      whTypeList,
      sideOk,
      isIDs,
      companyList,
      table1,
      whList,

      id,
      exports,
      tc1,

      gridOption,

      gridReady,

      total,
      dataForm,
      selectedRowKeys,
      s: selectedRowKeysChecked.selectedRowKeys,
      pagination,
      tableColumns,
      table,
      rowKey,
      pageData,
      selectHandler,
      defaultColDef,
      deleteHandler,
      headerShow,
      change1,
      PageSize,
    }
  },
  components: { Toolbar, PageTabelLayout, ModalDialog, QrcodeVue },
}
</script>
<style scoped lang="less">
:deep(.ag-cell) {
  border-right: 1px solid #dde2eb;
}

:deep(.ag-header-cell) {
  border-right: 1px solid #dde2eb;
}

:deep(.ag-cell-value) {
  user-select: initial;
  -moz-user-select: text;
  -ms-user-select: text;
  -webkit-user-select: text;
}
</style>
